package com.pantech.app.call_log_enabler;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class CallLogEnablerWidget extends AppWidgetProvider{

	private static final String LOGTAG = "callwidget";
	private static final String ACTION_ACTION_START = "com.pantech.action.start";
	private static final String ACTION_ACTION_STOP = "com.pantech.action.stop";
	private static final String ACTION_ACTION_ACTIVITY = "com.pantech.action.activity";
	
@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		Log.i(LOGTAG, "onUpdateStart");
        PendingIntent pending_start = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_ACTION_START), 0);
        PendingIntent pending_stop = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_ACTION_STOP), 0);
        PendingIntent pending_activity = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_ACTION_ACTIVITY), 0);
        
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.calllogwidget_layout);
        
        views.setOnClickPendingIntent(R.id.ImageBtn_icon_start, pending_start);
        views.setOnClickPendingIntent(R.id.ImageBtn_icon_stop, pending_stop);
        views.setOnClickPendingIntent(R.id.btn_activitirun_small, pending_activity);
        
        context.startService(new Intent(context, MyNumberService.class));
        
        ComponentName thisWidget = new ComponentName(context, CallLogEnablerWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, views);
        
	}
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
    }
    
    @Override
    public void onEnabled(Context context) {
        // TODO Auto-generated method stub
        super.onEnabled(context);
    }	
    
    @Override
    public void onReceive(Context context, Intent intent) {
    	String action = intent.getAction();
    	
    	if (action.equals(ACTION_ACTION_ACTIVITY)) 
    	{
    		Intent i = new Intent(context, MainActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
			context.startActivity(i);	
		}
    	if (action.equals(ACTION_ACTION_START)) 
    	{
			Toast.makeText(context, "onReceve :: ACTION_ACTION_START",Toast.LENGTH_SHORT).show();
			
			context.startService(new Intent(context, ProcessService.class));
			context.startService(new Intent(context, IconService.class));
		}
    	else if(action.equals(ACTION_ACTION_STOP))
		{
			Toast.makeText(context, "onReceve :: ACTION_ACTION_STOP",Toast.LENGTH_SHORT).show();
			context.stopService(new Intent(context, ProcessService.class));
			context.stopService(new Intent(context, IconService.class));
			Log.i(LOGTAG, "ACTION_ACTION_STOP");
    		Intent it = new Intent(context, ChangeFileNameActivity.class);
    		it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
			context.startActivity(it);	
/*			final Intent launchTaskAddIntent = new Intent(Intent.ACTION_VIEW);
            launchTaskAddIntent.setClass(context, ChangeFileNameActivity.class);
            final PendingIntent launchTaskAddPendingIntent = PendingIntent.getActivity(
                    context, 0, launchTaskAddIntent, 0);
            taskview.setOnClickPendingIntent(R.id.ImageBtn_icon_stop, launchTaskAddPendingIntent);*/
		}
    	super.onReceive(context, intent);
	}
    

}//end class_CallLogEnablerWidget
